package com.zmc.product.infrastructure.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zmc.common.utils.token.AuthTokenService;
import com.zmc.product.infrastructure.enums.ProductStatusEnum;
import com.zmc.common.utils.StringUtils;
import com.zmc.product.infrastructure.entity.*;
import com.zmc.product.infrastructure.service.*;
import com.zmc.product.infrastructure.bean.query.CommodityGoodsQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zmc.common.utils.PageUtils;

import com.zmc.product.infrastructure.dao.CommodityGoodsDao;
import org.springframework.transaction.annotation.Transactional;


/**
 * @author zmc
 */
@Service("commodityGoodsService")
public class CommodityGoodsServiceImpl extends ServiceImpl<CommodityGoodsDao, CommodityGoodsEntity> implements CommodityGoodsService {

    @Autowired
    private AuthTokenService authTokenService;

    @Override
    public PageUtils pageCommodityGoodsList(CommodityGoodsQuery query) {
        Page<CommodityGoodsEntity> page = baseMapper.selectPage(new Page<>(query.getPageNum(), query.getPageSize()), getWrapper(query));
        return new PageUtils(page);
    }

    @Override
    public List<CommodityGoodsEntity> selectCommodityGoodsList(CommodityGoodsQuery query) {
        return list(getWrapper(query));
    }

    @Override
    @Transactional
    public void goodToEditStatus(String spuId) {
        CommodityGoodsEntity goods = getBySpuId(spuId);
        Integer status = goods.getStatus();
        //若sku有任何改动，商品必须为编辑状态，由商户重新提审
        if (ProductStatusEnum.PRODUCT_AUDIT.getCode() != status) {
            goods.setStatus(ProductStatusEnum.PRODUCT_AUDIT.getCode());
            updateById(goods);
        }
    }

    @Override
    public CommodityGoodsEntity getBySpuId(String spuId) {
        LambdaQueryWrapper<CommodityGoodsEntity> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(CommodityGoodsEntity::getSpuId, spuId)
                .last(" limit 1");
        return getOne(wrapper);
    }

    @Override
    public Long countByMerchantId(String merchantId) {
        LambdaQueryWrapper<CommodityGoodsEntity> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(CommodityGoodsEntity::getMerchantId, merchantId);
        return count(wrapper);
    }

    @Override
    public List<CommodityGoodsEntity> listBySpuIds(List<String> spuIds) {
        LambdaQueryWrapper<CommodityGoodsEntity> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(CommodityGoodsEntity::getSpuId, spuIds);
        return list(wrapper);
    }


    private LambdaQueryWrapper<CommodityGoodsEntity> getWrapper(CommodityGoodsQuery query) {
        LambdaQueryWrapper<CommodityGoodsEntity> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(CommodityGoodsEntity::getMerchantId, authTokenService.getUserInfo().getMerchantId()).
                eq(StringUtils.isNotEmpty(query.getSpuId()), CommodityGoodsEntity::getSpuId, query.getSpuId())
                .eq(StringUtils.isNotEmpty(query.getName()), CommodityGoodsEntity::getName, query.getName())
                .eq(query.getStatus() != null, CommodityGoodsEntity::getStatus, query.getStatus())
                .ge(StringUtils.isNotEmpty(query.getBeginTime()), CommodityGoodsEntity::getCreateTime, query.getBeginTime())
                .le(StringUtils.isNotEmpty(query.getEndTime()), CommodityGoodsEntity::getCreateTime, query.getEndTime())
                .orderByDesc(CommodityGoodsEntity::getCreateTime);
        return wrapper;
    }


}